package com.promise.jokerdream.work.knight.lnn

import com.promise.jokerdream.database.entity.KnightGameConfigEntity
import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 侠士客栈工作
class KnightLnnWork(
    private val config: KnightGameConfigEntity,
    uin: String
): BaseWork(uin) {
    
    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }
    
    override val name: String
        get() = "侠士客栈"

    override suspend fun executeWork(): WorkResult {
        return try {
            // 1. 查询侠士客栈大堂信息
            val lobbyResult = gameApi.queryKnightLnnLobby()
            lobbyResult.fold(
                onSuccess = { lobbyResponse ->
                    // 2. 检查大堂是否开业中
                    if (lobbyResponse.isOpen == 0) {
                        // 大堂关闭能领奖，检查哪些奖励可以领取（draw[i] = 0表示未领取）
                        lobbyResponse.draw.forEachIndexed { index, drawStatus ->
                            if (drawStatus == 0) {
                                // 可以领取，执行领取操作
                                val awardId = index + 1 // 奖励ID从1开始
                                val claimResult = gameApi.getKnightLnnAward(awardId, 1)
                                claimResult.fold(
                                    onSuccess = { claimResponse ->
                                        historyManager.addHistory(name, claimResponse.getDescription())
                                    },
                                    onFailure = { exception ->
                                        historyManager.addHistory(name, exception.message)
                                    }
                                )
                            }
                        }
                    }
                },
                onFailure = { exception ->
                    historyManager.addHistory(name, exception.message)
                }
            )
            
            // 3. 查询侠士客栈房间信息
            val roomResult = gameApi.queryKnightLnnRoom()
            roomResult.fold(
                onSuccess = { roomResponse ->
                    // 4. 检查房间是否开业中
                    if (roomResponse.isOpen == 0) {
                        // 房间关闭可以领奖，检查哪些房间奖励可以领取
                        roomResponse.room.forEachIndexed { index, room ->
                            if (room.draw == 0) {
                                // 可以领取房间奖励，执行领取操作
                                val roomId = index + 1 // 房间ID从1开始
                                val claimRoomResult = gameApi.getKnightLnnAward(roomId, 2)
                                claimRoomResult.fold(
                                    onSuccess = { claimResponse ->
                                        historyManager.addHistory(name, claimResponse.getDescription())
                                    },
                                    onFailure = { exception ->
                                        historyManager.addHistory(name, exception.message)
                                    }
                                )
                            }
                        }
                    }
                },
                onFailure = { exception ->
                    historyManager.addHistory(name, exception.message)
                }
            )
            
            WorkResult.Success
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }
}